package org.edu.modules.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.modules.base.entity.VeBaseSysMenu;
import org.edu.modules.base.service.IVeBaseAppUserService;
import org.edu.modules.base.service.IVeBaseSysMenuService;
import org.edu.modules.base.service.IVeBaseSysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = {"菜单管理"})
@RestController
@RequestMapping("/base/VeBaseSysMenu")
@ApiSort(4)
public class VeBaseSysMenuController {

    @Autowired
    private IVeBaseSysMenuService veBaseSysMenuService;

    @Autowired
    private IVeBaseSysRoleMenuService veBaseSysRoleMenuService;

    @Autowired
    private IVeBaseAppUserService iVeBaseAppUserService;

    @AutoLog("菜单-树状")
    @ApiOperation(value = "树状菜单", notes = "树状菜单")
    @GetMapping({"/getSysMenuTreeList"})
    public Result<?> getSysMenuTreeList(@RequestParam(name = "id", defaultValue = "") String id) {
        List<Map<String, Object>> list = this.veBaseSysMenuService.getSysMenuTreeList(id);
        return Result.ok(list);
    }

    @AutoLog("总菜单-树状")
    @ApiOperation(value = "树状菜单", notes = "树状菜单")
    @GetMapping({"/getAllSysMenuTreeList"})
    public Result<?> getAllSysMenuTreeList(@RequestParam(name = "userId") String userId) {
//        List<Map<String,Object>> appUserList = iVeBaseAppUserService.getAppUserByUserId(userId,"");
//        LambdaQueryWrapper<VeBaseAppUser> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(VeBaseAppUser::getUserId, userId);
//        VeBaseAppUser veBaseAppUser = iVeBaseAppUserService.getOne(queryWrapper);
//        if (veBaseAppUser != null ) {
//            veBaseAppUser.getRoleId();
//        }
        if (userId!=null) {
            if (userId.equals("admin")){
                userId = null;
            }
        }
        List<JSONObject> list = this.veBaseSysMenuService.getAllSysMenuTreeList(userId,"0");




        return Result.ok(list);
    }

    @AutoLog("菜单-添加")
    @ApiOperation(value = "添加菜单", notes = "添加菜单")
    @PostMapping({"/addSysMenu"})
    public Result<?> addSysMenu(@RequestBody VeBaseSysMenu veBaseSysMenu) {
        veBaseSysMenu.setPath(null);
        Integer id = this.veBaseSysMenuService.addSysMenu(veBaseSysMenu);
        veBaseSysMenu.setId(veBaseSysMenu.getId());
        if (veBaseSysMenu.getPid().intValue() == 0) {
            veBaseSysMenu.setPid(Integer.valueOf(0 - veBaseSysMenu.getId().intValue()));
            veBaseSysMenu.setPath(0 - veBaseSysMenu.getId().intValue() + "." + veBaseSysMenu.getId() + ".");
            this.veBaseSysMenuService.updateById(veBaseSysMenu);
        } else {
            VeBaseSysMenu model = this.veBaseSysMenuService.getSysMenuByPid(veBaseSysMenu.getPid());
            if (model != null) {
                if (model.getPid().intValue() < 0) {
                    veBaseSysMenu.setPath(model.getPid() + "." + model.getId() + "." + veBaseSysMenu.getId() + ".");
                    this.veBaseSysMenuService.updateById(veBaseSysMenu);
                } else {
                    VeBaseSysMenu model1 = this.veBaseSysMenuService.getSysMenuByPid(model.getPid());
                    if ((model1 != null) &&
                            (model1.getPid().intValue() < 0)) {
                        veBaseSysMenu.setPath(model.getPid() + "." + model.getId() + "." + model1.getId() + "." + veBaseSysMenu.getId() + ".");
                        this.veBaseSysMenuService.updateById(veBaseSysMenu);
                    }
                }
            }
        }
        return Result.ok("添加成功!");
    }

    @AutoLog("菜单-编辑")
    @ApiOperation(value = "编辑菜单", notes = "编辑菜单")
    @PostMapping({"/editSysMenuById"})
    public Result<?> editSysMenu(@RequestBody VeBaseSysMenu veBaseSysMenu) {
        this.veBaseSysMenuService.updateById(veBaseSysMenu);
        return Result.ok("修改成功!");
    }

    @AutoLog("菜单-通过id删除")
    @ApiOperation(value = "菜单删除-通过id删除", notes = "菜单删除-通过id删除")
    @PostMapping({"/deleteSysMenuById"})
    @Transactional
    public Result<?> deleteSysMenu(@RequestParam(name = "id", required = true) String id) {
        //真删除removeById
        this.veBaseSysMenuService.removeById(id);
        this.veBaseSysRoleMenuService.deleteRoleMenuByMenuId(id);
        return Result.ok("删除成功!");
    }

    @AutoLog("菜单-批量删除")
    @ApiOperation(value = "菜单-批量删除", notes = "菜单-批量删除")
    @PostMapping({"/menuDeleteBatch"})
    public Result<?> menuDeleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.veBaseSysMenuService.removeByIds(list);
        if (list.size() > 0) {
            for (String id : list) {
                this.veBaseSysRoleMenuService.deleteRoleMenuByMenuId(id);
            }
        }
        return Result.ok("批量删除成功!");
    }
}
